package org.dwpd.config;

import lombok.extern.slf4j.Slf4j;
import org.dwpd.controller.system.WebSocketController;
import org.dwpd.interceptor.CustomHandshakeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketController webSocketController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 通过这个方法，把刚刚创建好的 Handler 类给注册到具体的路径上
        // 此时当浏览器发起的 websocket 请求路径是 "/WebSocketMessage"的时候，就会调用到WebSocketController

        registry.addHandler(webSocketController, "/WebSocketMessage")
                .addInterceptors(new CustomHandshakeInterceptor())
                .setAllowedOrigins("https://wutma.cn", "https://www.wutma.cn"); // 解决拦截器拦截问题，大坑，终于解决
//                .setAllowedOrigins("*");
//        .addInterceptors(new HttpSessionHandshakeInterceptor());
        // 通过注册这个特定的 HttpSession 拦截器，就可以把用户给 HttpSession 中添加的 Attribute 键值对
        // 往我们的 WebSocketSession 里也添加一份

    }
}
